<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;
use Carbon\Carbon;

class PartTimeResume extends ApiModel
{
    protected $table = 'part_time_resume';

    protected $hidden = [
        'part_time_resume_free_time', 'area_place', 'town_place',
        'area_address', 'town_address'
    ];

    protected $casts = [
        'id' => 'string', 'uid' => 'string', 'sex' => 'string',
        'address' => 'string', 'student' => 'string',
        'height' => 'string', 'education' => 'string',
        'place' => 'string',
    ];

    public function part_time_resume_free_time()
    {
        return $this->hasOne(PartTimeResumeFreeTime::class, 'bt_id');
    }

    public function getFreeTimeAttribute()
    {
        //获取兼职时间表
        $time = $this->getRelationValue('part_time_resume_free_time');
        if (is_null($time)) {
            return '';
        }

        $arr = [];
        for ($i = 1; $i < 22; $i++) {
            $key = 'num' . $i;
            $time->$key != 0 && $arr[] = $i;
        }
        return implode(',', $arr);
    }

    /** 兼职时间转为关联数组
     * @param $value
     * @return array
     */
    public static function changeFreeTime($value)
    {
        $value = is_array($value) ? $value : explode(',', $value);

        $arr = [];
        for ($i = 1; $i < 22; $i++) {
            $key = 'num' . $i;
            $arr[$key] = in_array($i, $value) ? 1 : 0;
        }
        return $arr;
    }

    //-----------------------获取place城市名--------------------------
    /** area表
     * @return mixed
     */
    public function area_place()
    {
        return $this->hasOne(CollArea::class, 'cid', 'place')
            ->select('cid', 'name');
    }

    /** town表
     * @return mixed
     */
    public function town_place()
    {
        return $this->hasOne(CollTown::class, 'cid', 'place')
            ->select('cid', 'name');
    }

    /**
     * @return string
     */
    public function getPlaceNameAttribute()
    {
        $c_id = $this->attributes['place'];
        if ($c_id == 0) return '不限';

        if (strlen(strval($c_id)) > 7) {
            $c = $this->getRelationValue('town_place');
            return $c ? $c->name : '';
        }

        $c = $this->getRelationValue('area_place');
        return $c ? $c->name : '';
    }
    //-----------------------end------------------------------------------

    //-----------------------获取address城市名--------------------------
    /** area表
     * @return mixed
     */
    public function area_address()
    {
        return $this->hasOne(CollArea::class, 'cid', 'address')
            ->select('cid', 'name');
    }

    /** town表
     * @return mixed
     */
    public function town_address()
    {
        return $this->hasOne(CollTown::class, 'cid', 'address')
            ->select('cid', 'name');
    }

    /**
     * @return string
     */
    public function getAddressNameAttribute()
    {
        $c_id = $this->attributes['address'];
        if ($c_id == 0) return '';

        if (strlen(strval($c_id)) > 7) {
            $c = $this->getRelationValue('town_address');
            return $c ? $c->name : '';
        }

        $c = $this->getRelationValue('area_address');
        return $c ? $c->name : '';
    }
    //-----------------------end------------------------------------------

    /** 学历要求
     * @return string
     */
    public function getEducationNameAttribute()
    {
        if ($this->attributes['education'] == 0) {
            return '';
        }
        return CollOption::getOptionName($this->attributes['education'], 'education');
    }

    public function getBirthdayNameAttribute()
    {
        return substr($this->attributes['birthday'], 0, 7);
    }

    public function getHeightAttribute($v)
    {
        return $v == 0 ? '' : strval($v);
    }

    public function getTimeStartAttribute($v)
    {
        return $v == '0000-00-00' ? '' : $v;
    }

    public function getTimeStartNameAttribute()
    {
        $t = $this->attributes['time_start'];
        return $t == '0000-00-00' ? '' : substr($t, 0, 7);
    }

    public function getTimeEndAttribute($v)
    {
        return $v == '0000-00-00' ? '' : $v;
    }

    public function getTimeEndNameAttribute()
    {
        $t = $this->attributes['time_end'];
        return $t == '0000-00-00' ? '' : substr($t, 0, 7);
    }

    public function getIntentionNameAttribute()
    {
        $idx = explode(',', $this->attributes['intention']);
        $arr = [];
        foreach ($idx as $item) {
            $arr[] = CollOption::getOptionName($item, 'part_time_type');
        }
        return implode(',', $arr);
    }

    public function getTimeAttribute()
    {
        return substr($this->attributes['updated_at'], 0, 10);
    }

    public function getAgeAttribute()
    {
        return Carbon::parse($this->attributes['birthday'])->age;
    }
}
